package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.IProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品规格")
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {
    @Autowired
    IProductSpecService productSpecService;

    /**
     * 商品规格的分页
     *
     * @param productSpec
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("list")
    public TableDataInfo list(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> productSpecList = productSpecService.selectAll(productSpec);
        return getDataTable(productSpecList);
    }

    /**
     * 商品规格详细信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "商品规格详细信息")
    @GetMapping("/{id}")
    public AjaxResult getProductSpecById(@PathVariable("id") Long id) {
        return success(productSpecService.selePoductSpecById(id));
    }


    /**
     * 增加商品规格
     *
     * @param productSpec
     * @return
     */
    @Operation(summary = "增加商品规格")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ProductSpec productSpec) {
        return toAjax(productSpecService.save(productSpec));
    }

    /**
     * 修改商品规格
     *
     * @param productSpec
     * @return
     */
    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpec productSpec) {
        return toAjax(productSpecService.updateById(productSpec));
    }

    /**
     * 删除商品规格
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除商品规格")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") List<Long> ids) {
        return toAjax(productSpecService.removeByIds(ids));
    }

    /**
     * 根据分类id获取商品规格列表
     *
     * @param categoryId
     * @return
     */
    @Operation(summary = "根据分类id获取商品规格列表")
    @GetMapping("/productSpecList/{categoryId}")
    public AjaxResult queryAllByCategoryId(@PathVariable("categoryId") Long categoryId) {
        return success(productSpecService.selectProductSpecListByCategoryId(categoryId));
    }
}
